package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type WaaddBody struct {
	ID      int64  `json:"id" gorm:"id"`
	Name    string `json:"name" gorm:"name"`
	Phone   string `json:"phone" gorm:"phone"`
	Title   string `json:"title" gorm:"title"`
	Beizhu  string `json:"beizhu" gorm:"beizhu"`
	Addtime int64  `json:"addtime" gorm:"addtime"`
	Uid     int64  `json:"uid" gorm:"uid"`
}

func Wabeizhu(ctx *gin.Context) {
	request := WaaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	beizhu := request.Beizhu

	data_source.Db.Model(&models.Wa{}).Where("id = ?", id).Update("beizhu", beizhu)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Wachuli(ctx *gin.Context) {
	request := WaaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Model(&models.Wa{}).Where("id = ?", id).Update("status", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Wainfo(ctx *gin.Context) {
	request := WaaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var yxb models.Wa
	data_source.Db.Where("id = ?", id).First(&yxb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    yxb,
		"msg":     "ok",
	})
	return

}
func Waadd(ctx *gin.Context) {
	request := WaaddBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	phone := request.Phone
	title := request.Title
	name := request.Name

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    300,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请传递uid",
		})
		return
	}

	fk := models.Wa{
		Uid:     uid,
		Phone:   phone,
		Title:   title,
		Addtime: time.Now().Unix(),
		Name:    name,
		Status:  1,
	}
	data_source.Db.Create(&fk)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type WalistRBody struct {
	Data       []models.Wasql `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}

type WalistBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Inttime  int64 `json:"inttime" from:"inttime"`
	Endtime  int64 `json:"endtime" from:"endtime"`
	Uid      int64 `json:"uid" from:"uid"`
	Status   int64 `gorm:"column:status" json:"status"` //1添加审核中 2审核通过 3拒绝 4修改审核中
	Classify int64 `json:"classify" gorm:"classify"`    // 1合作 2采购 3询价 4招募经销商 4原材料供应
}

func Walist(ctx *gin.Context) {
	request := WalistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	status := request.Status
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Wasql

	res := data_source.Db.Model(&models.Wa{}).Select("wa.*, usersj.nickname").Joins("left join usersj on usersj.id = wa.uid").Where("wa.uid = ?", uid)
	if status > 0 {
		res = res.Where("wa.status = ?", status)
	}
	res.Model(&models.Wa{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := WalistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Was(ctx *gin.Context) {
	request := WalistBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	inttime := request.Inttime
	endtime := request.Endtime
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Wasql
	res := data_source.Db.Model(&models.Wa{}).Select("wa.*, usersj.nickname").Joins("left join usersj on usersj.id = wa.uid")
	if status > 0 {
		res = res.Where("yingxiaobao.status = ?", status)
	}
	if inttime > 0 {
		res = res.Where("yingxiaobao.addtime >= ?", inttime)
	}
	if endtime > 0 {
		res = res.Where("yingxiaobao.addtime < ?", endtime)
	}
	res.Model(&models.Wa{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := WalistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Walogadd(ctx *gin.Context) {
	request := WalistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	walog := models.Walog{
		Uid:     uid,
		Addtime: time.Now().Unix(),
	}
	data_source.Db.Create(&walog)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
